﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;


public class Camera2d
{
    #region Camera2d declarations

    protected float _zoom; // Camera Zoom
    public Matrix _transform; // Matrix Transform
    public Vector2 _pos; // Camera Position
    protected float _rotation; // Camera Rotation
    private GraphicsDevice _graphicDevice;


    #endregion

    #region Camera2d constructors

    public Camera2d(GraphicsDevice graphicDevice)
    {
        _zoom = 1.0f;
        _rotation = 0.0f;
        _pos = Vector2.Zero;
        _graphicDevice = graphicDevice;
    }

    #endregion

    #region Camera2d properties

    public float Zoom
    {
        get { return _zoom; }
        set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
    }

    public float Rotation
    {
        get { return _rotation; }
        set { _rotation = value; }
    }

    public Vector2 Pos
    {
        get { return _pos; }
        set { _pos = value; }
    }

    #endregion

    #region Camera2d auxiliary

    public void Move(Vector2 amount)
    {
        _pos += amount;
    }

    public Matrix get_transformation(GraphicsDevice graphicsDevice) 
    {
        _transform =       // Thanks to o KB o for this solution
          Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
                                     Matrix.CreateRotationZ(Rotation) *
                                     Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                     Matrix.CreateTranslation(new Vector3(0 , 0 , 0));
        return _transform;
    }

    //gets a position on the screen (limited to screen size) and returns to position in the whole game world
    public Vector2 screenToWorld(Vector2 screenPosition)
    {
        Vector2 ans = new Vector2(_pos.X  + screenPosition.X, _pos.Y + screenPosition.Y);
        return ans;
    }

    public Rectangle screenToWorld(Rectangle screenPosition)
    {
        Rectangle ans = new Rectangle((int)(_pos.X + screenPosition.X), (int)(_pos.Y + screenPosition.Y), screenPosition.Width, screenPosition.Height);
        return ans;
    }

    public Vector2 WorldToScreen(Vector2 screenPosition)
    {
        Vector2 ans = new Vector2(screenPosition.X - _pos.X, screenPosition.Y - _pos.Y);
        return ans;
    }

    public Rectangle WorldToScreen(Rectangle screenPosition)
    {
        Rectangle ans = new Rectangle((int)(screenPosition.X - _pos.X), (int)(screenPosition.Y - _pos.Y), screenPosition.Width, screenPosition.Height);
        return ans;
    }

    #endregion

    
}